﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Linq.Expressions;

using CanDoo.Core;
using CanDoo;
using CanDoo.Data;
using CanDoo.Web;

using CanDoo.Oil.Core.Models;
using CanDoo.Oil.Core.Contracts;
using FineUIPro;
using CanDoo.FineUI;
using CanDoo.Extensions;

namespace CanDoo.Oil.WebApp.sysDepartment
{
    public partial class Edit : CanDoo.FineUI.TreeEditPagebase<IDepartmentContract, Department>
    {

        #region PagePower

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string PageCode
        {
            get
            {
                return "sysdepartment";
            }
        }

        /// <summary>
        /// 本页面的浏览权限，空字符串表示本页面不受权限控制
        /// </summary>
        public override string ViewPower
        {
            get
            {
                return "sysdepartmentview";
            }
        }

        /// <summary>
        /// 本页面的功能模块
        /// </summary>
        public override string PageTitle
        {
            get
            {
                return "系统部门";
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //var TreeList = FineUITreeHelper.ResolveDDL<Department>(Manager.GetResolvedTree(), EntityId);
                //FineUIListHelper.BindTreeDDL<Department>(TreeList, 0L, ed_Parent);

                var TreeList = Manager.GetSelectList();
                //FineUIListHelper.BindTreeDDL(TreeList, 0L, ed_Parent);
                FineUIListHelper.BindDDL(Manager.EntityName,TreeList, 0L, ed_Parent);

                LoadData();
                if (PageMode == pagemode.Add)
                {
                    //GetSortIndex(TreeList);

                    var parentId = ed_Parent.GetValue();
                    var parentNode = TreeList.FirstOrDefault(p => p.Value == parentId);
                    if(parentNode != null)
                        this.ed_SortIndex.SetValue(TreeList.Count(p => p.NodeCode.StartsWith(parentNode.NodeCode, StringComparison.Ordinal)));
                    else
                        this.ed_SortIndex.SetValue(TreeList.Count());
                }

            }
        }

        protected override void LoadData(Department Entity)
        {
            ed_Code.SetValue(Entity.Code);
            ed_Name.SetValue(Entity.Name);
            ed_SortIndex.SetValue(Entity.SortIndex);
            ed_Remark.SetValue(Entity.Remark);
            if(!Entity.Parent.IsNull())
                ed_Parent.SetValue(Entity.ParentId);

        }

        protected override OperationResult SaveData(Department entity)
        {
            entity.Code = ed_Code.GetValue();
            entity.Name = ed_Name.GetValue();
            entity.SortIndex = ed_SortIndex.GetValue().ToInt();
            entity.Remark = ed_Remark.GetValue();
            if (ed_Parent.GetValue().ToLong() == 0L)
                entity.ParentId = null;
            else
                entity.ParentId = ed_Parent.GetValue().ToLong();
            return base.SaveData(entity);
        }
    }
}
